package com.ftg.learn.mall.mapper;

import com.ftg.learn.mall.dto.PmsProductCategoryParam;
import com.ftg.learn.mall.dto.PmsProductCategoryWithChildrenItem;
import com.ftg.learn.mall.entity.PmsProductCategory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 产品分类 Mapper 接口
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
public interface PmsProductCategoryMapper extends BaseMapper<PmsProductCategory> {


    /**
     * 当添加了一个商品的时候，对应其产品表中的商品信息数量+1
     * @param cid
     * @return
     */
    @Update("update pms_product_category set product_count = product_count + 1 " +
            "where product_category_id = #{cid}")
    int updateProductCount(@Param("cid") Long cid);

    /**
     * 根据产品分类id查询出此品牌的名称，并在添加一件商品信息时自动赋予到其产品分类名称字段
     * @param cid
     * @return
     */
    @Select("select product_category_name from pms_product_category where product_category_id = #{cid}")
    String findOneCategoryName(@Param("cid") Long cid);

    /**
     * 根据id获取父级下子级分类
     * @return
     */
    @Select("select\n" +
            "                    c2.product_category_id,\n" +
            "                    c2.product_category_level,c2.product_category_name, \n" +
            "                    c2.product_count,\n" +
            "                    c2.product_unit,\n" +
            "                    c2.nav_status,\n" +
            "                    c2.show_status,\n" +
            "                    c2.sort\n" +
            "            from pms_product_category c1 \n" +
            "            left join pms_product_category c2 on c1.product_category_id = \n" +
            "            c2.parent_id WHERE  c1.product_category_id = #{param} and c1.show_status=1")
    List<PmsProductCategoryParam> findParentAndChild(Long id);


    /**
     * 修改状态栏显示
     * @param ids
     * @return
     */
    @Update("UPDATE pms_product_category SET nav_status = #{navStatus} WHERE product_category_id = #{id}")
    int updateNavStatus(@Param("id") Long ids,@Param("navStatus") Long navStatus);

    /**
     * 修改显示状态
     * @param ids
     * @return
     */
    @Update("UPDATE pms_product_category SET show_status = #{showStatus} WHERE product_category_id = #{id}")
    int updateShowStatus(@Param("id") Long ids, @Param("showStatus") Long showStatus);


    /**
     *  查询所有产品类型一级分类及子分类
     * @return
     */

    @Select("select * from pms_product_category where parent_id = 0")
    @Results({
            @Result(property = "productCategoryId",column = "product_category_id"),
            @Result(property = "children",column = "product_category_id",javaType = List.class,
            many = @Many(select = "findCategoryChildren") )
    })
    List<PmsProductCategoryWithChildrenItem> findWithChildren();

    /**
     * 查询出二级分类的产品类型
     * @param pid
     * @return
     */
    @Select("select * from pms_product_category where parent_id = #{pid}")
    List<PmsProductCategory> findCategoryChildren(@Param("pid") Long pid);


    /**
     *  查询出所有产品类型数据
     * @return
     */
    @Select("select * from pms_product_category")
    List<PmsProductCategoryWithChildrenItem> findAllProductCategory();


}
